package com.tentative.competition.framework.config;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.tentative.competition.common.core.domain.model.LoginUser;
import com.tentative.competition.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * MP注入处理器
 *
 * @author llh
 * @date 2022/5/13
 */
@Component
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {


    private final static String CREATE_TIME = "createTime";
    private final static String UPDATE_TIME = "updateTime";
    private final static String CREATE_BY = "createBy";
    private final static String UPDATE_BY = "updateBy";

    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            if (metaObject.hasGetter(CREATE_TIME) && metaObject.getValue(CREATE_TIME) == null) {
                this.setFieldValByName(CREATE_TIME, new Date(), metaObject);
            }
            //获取当前登录用户
            LoginUser loginUser = getLoginUser();
            if(null != loginUser && null != loginUser.getUserId() && metaObject.hasGetter(CREATE_BY) && metaObject.hasGetter(UPDATE_BY)){
                this.setFieldValByName(CREATE_BY, loginUser.getUserId(), metaObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            if (metaObject.hasGetter(UPDATE_TIME) && metaObject.getValue(UPDATE_TIME) == null) {
                this.setFieldValByName(UPDATE_TIME, new Date(), metaObject);
            }
            //获取当前登录用户
            LoginUser loginUser = getLoginUser();
            if(null != loginUser && null != loginUser.getUserId() && metaObject.hasGetter(UPDATE_BY)){
                this.setFieldValByName(UPDATE_BY, loginUser.getUserId(), metaObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Bean
//    public MybatisPlusInterceptor mybatisPlusInterceptor() {
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        // 分页插件
//        interceptor.addInnerInterceptor(paginationInnerInterceptor());
//        return interceptor;
//    }
//
//    /**
//     * 分页插件，自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
//     */
//    public PaginationInnerInterceptor paginationInnerInterceptor() {
//        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//        // 设置数据库类型为mysql
//        paginationInnerInterceptor.setDbType(DbType.MYSQL);
//        // 设置最大单页限制数量，默认 500 条，-1 不受限制
//        paginationInnerInterceptor.setMaxLimit(-1L);
//        return paginationInnerInterceptor;
//    }

    private LoginUser getLoginUser(){
        try {
            return SecurityUtils.getLoginUser();
        }catch (Exception e){
            return null;
        }
    }


}
